(function (root, factory) {
    'use strict'
    if(typeof define === "function" && define.amd) {
        define(["Wolf"], factory);
    }
    else if(typeof module === "object" && module.exports) {
        var Wolf;
        try {
            Wolf = require("Wolf");
        }
        catch (err) {
            Wolf = null;
        }
        module.exports = factory(jQuery);
    }
    else {
        root.Wolf = factory();
    }
})(this, function () {
    'use strict'

    function Wolf() {
    };

    Wolf.prototype = {
        get: function (url, data, callback) {
            $.ajax({
                type: 'GET',
                url: url,
                data: data,
                success: function (response, status, xhr) {
                    if (callback) {
                        callback(response, status, xhr);
                    }
                },
                dataType: 'json'
            });
        },
        getSynch: function (url, data, callback) {
            Wolf.prototype.showLoading();
            $.ajax({
                type: 'GET',
                async: false,
                url: url,
                data: data,
                success: function (response, status, xhr) {
                    Wolf.prototype.hideLoading();
                    if (callback) {
                        callback(response, status, xhr);
                    }
                },
                error: function (response) {
                    console.log(response);
                    Wolf.prototype.hideLoading();
                },
                dataType: 'json'
            });
        },
        post: function (url, data, callback) {
            $.ajax({
                type: 'POST',
                url: url,
                data: data,
                success: function (response, status, xhr) {
                    if (callback) {
                        callback(response, status, xhr);
                    }
                },
                dataType: 'json'
            });
        },
        postSynch: function (url, data, callback) {
            Wolf.prototype.showLoading();
            $.ajax({
                type: 'POST',
                async: false,
                url: url,
                data: data,
                success: function (response, status, xhr) {
                    Wolf.prototype.hideLoading();
                    if (callback) {
                        callback(response, status, xhr);
                    }
                },
                error: function (response) {
                    console.log(response);
                    Wolf.prototype.hideLoading();
                },
                dataType: 'json'
            });
        },

        showLoading: function(){
            var html = '<div class="wolf-loading-mask"><div class="overlay"><i class="fa fa-refresh fa-spin"></i></div></div>';
            $("body").append(html);
        },
        hideLoading: function(){
            $(".wolf-loading-mask").remove();
        },

        alert: function (message, title, callback) {
            var html =
                '<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" data-backdrop="static" id="wolf-alert">'
                + '  <div class="modal-dialog modal-sm" role="document">'
                + '    <div class="modal-content">'
                + '      <div class="modal-header">'
                + '        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>'
                + '        <h4 class="modal-title">' + (title?title:'') + '</h4>'
                + '      </div>'
                + '      <div class="modal-body">' + (message?message:'') +'</div>'
                + '      <div class="modal-footer">'
                + '         <button type="button" class="btn btn-primary" data-dismiss="modal">确定</button>'
                + '      </div>'
                + '    </div>'
                + '  </div>'
                + '</div>';
            $("body").append(html);
            $("#wolf-alert").modal();
            $('#wolf-alert').on('hidden.bs.modal', function (e) {
                $("#wolf-alert").remove();
                if(callback) callback();
            })
        },

        confirm: function (message, callback) {
            var html =
                '<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" data-backdrop="static" id="wolf-confirm">'
                + '  <div class="modal-dialog modal-sm" role="document">'
                + '    <div class="modal-content">'
                + '      <div class="modal-header">'
                + '        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>'
                + '        <h4 class="modal-title">请确认</h4>'
                + '      </div>'
                + '      <div class="modal-body">' + (message?message:'') +'</div>'
                + '      <div class="modal-footer">'
                + '         <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>'
                + '         <button type="button" class="btn btn-primary" id="wolf-confirm-button">确定</button>'
                + '      </div>'
                + '    </div>'
                + '  </div>'
                + '</div>';
            $("body").append(html);
            $("#wolf-confirm").modal();
            $('#wolf-confirm').on('hidden.bs.modal', function (e) {
                $("#wolf-confirm").remove();
            })
            $("#wolf-confirm-button").on('click', function(e){
                $('#wolf-confirm').modal('hide');
                if(callback) callback();
            })
        }

    };

    return new Wolf();
})